/**********************************************
 * 
 * File Name				:	EmailHelper.java
 * 
 * Last Changed Date		: 	$Date: 2009-08-01 11:12:53 +0800 (星期六, 01 八月 2009) $
 * 
 * Last Changed Revision	:	$Revision: 86 $
 * 
 * Last Changed Author		:	$Author: cfanboy $
 * 
 * File Header URL			:	$HeadURL: svn://www.svnhost.cn/Busi-Building/code/branch/src/com/busibuilding/common/email/EmailHelper.java $
 * 
 * ********************************************
 */

package com.busibuilding.common.email;

import javax.mail.BodyPart;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import com.busibuilding.utils.DateUtils;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.core.task.TaskExecutor;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;

import com.busibuilding.common.Constants;
import com.busibuilding.common.vo.EmailVO;
import com.busibuilding.utils.StringUtil;

/**
 * Class Name: EmailHelper.java Description: The helper will be used for email.
 * 
 * @author YU Zhipeng
 * @version 1.0
 * @since 1.0 Date: 2009-7-13
 */

public class EmailHelper implements Constants {
	private static final Logger logger = LogManager.getLogger(EmailHelper.class);
	
//	private static EmailHelper instance;
	private JavaMailSender sender;
	
	private TaskExecutor taskExecutor;

	public void setSender(JavaMailSender sender) {
		this.sender = sender;
	}
	
	public void setTaskExecutor(TaskExecutor taskExecutor) {
		this.taskExecutor = taskExecutor;
	}

	/**
	 * Send the email, if the content in EmailVO is html format, 
	 * then send html email or else send simple text email with synchronous.
	 * Avoid use this method.
	 * @param vo
	 * @throws Exception
	 * 
	 */
	public void sendMail(EmailVO vo, boolean isHtml) throws Exception{
		SimpleMailMessage msg = new SimpleMailMessage();
		if (StringUtil.isNotEmpty(vo.getFrom())){
			msg.setFrom(vo.getFrom());
		}
		if (StringUtil.isNotEmpty(vo.getTo())){
			msg.setTo(vo.getTo());
		}
		if (StringUtil.isNotEmpty(vo.getReplyTo())){
			msg.setReplyTo(vo.getReplyTo());
		}
		if (StringUtil.isNotEmpty(vo.getCc())){
			msg.setCc(vo.getCc());
		}
		
		if (StringUtil.isNotEmpty(vo.getSubject())){
			msg.setSubject(vo.getSubject());
		}
		if (isHtml){
			if (StringUtil.isNotEmpty(vo.getHtmlContent())){
				msg.setText(vo.getHtmlContent());
			}
		}else{
			if (StringUtil.isNotEmpty(vo.getPlainTextConent())){
				msg.setText(vo.getPlainTextConent());
			}
		}
		
		if (StringUtil.isNotEmpty(vo.getSendDate())){
			msg.setSentDate(DateUtils.parseDate(vo.getSendDate(), DATE_PATTERN));
		}
		
		sender.send(msg);
	}
	
	/**
	 * Send mail with asynchronous
	 * @param vo
	 * @param isHtml
	 * @throws Exception
	 */
	public void sendAsyncMail(final EmailVO vo, final boolean isHtml) throws Exception{
		taskExecutor.execute(new Runnable(){
			public void run(){
				try {
					sendMail(vo, isHtml);
				} catch (Exception e) {
					logger.fatal(e);
				}
			}
		});
	}
	
	/**
	 * Send html email and plain text email with synchronous.
	 * Avoid use this method.
	 * @param vo
	 * @throws Exception
	 */
	public void sendAlternativeMail(final EmailVO vo) throws Exception{
		MimeMessagePreparator mmp = new MimeMessagePreparator(){
			public void prepare(MimeMessage msg) throws Exception{
				MimeMessageHelper helper = new MimeMessageHelper(msg, true, ENCODING_FORMAT);
				if (StringUtil.isNotEmpty(vo.getFrom())){
					helper.setFrom(vo.getFrom());
				}
				
				if (StringUtil.isNotEmpty(vo.getTo())){
					helper.setTo(vo.getTo());
				}
				
				if (StringUtil.isNotEmpty(vo.getSubject())){
					helper.setSubject(vo.getSubject());
				}
				
				if (StringUtil.isNotEmpty(vo.getReplyTo())){
					helper.setReplyTo(vo.getReplyTo());
				}
				
				if (StringUtil.isNotEmpty(vo.getCc())){
					helper.setCc(vo.getCc());
				}
				
				if (StringUtil.isNotEmpty(vo.getSubject())){
					helper.setSubject(vo.getSubject());
				}
				
				if (StringUtil.isNotEmpty(vo.getSendDate())){
					helper.setSentDate(DateUtils.parseDate(vo.getSendDate(), DATE_PATTERN));
				}
				
				MimeMultipart mmPart = new MimeMultipart("alternative");
				msg.setContent(mmPart);
				
				BodyPart plainTextPart = new MimeBodyPart();
				if (StringUtil.isNotEmpty(vo.getPlainTextConent())){
					plainTextPart.setText(vo.getPlainTextConent());
				}
				mmPart.addBodyPart(plainTextPart);
				
				BodyPart htmlPart = new MimeBodyPart();
				if (StringUtil.isNotEmpty(vo.getHtmlContent())){
					htmlPart.setContent(vo.getHtmlContent(), "text/html;charset=UTF-8");
				}
				mmPart.addBodyPart(htmlPart);
			}
		};
		sender.send(mmp);
	}
	
	/**
	 * Send html and plain text email with asynchronous
	 * @param vo
	 * @throws Exception
	 */
	public void sendAsyncAlternativeMail(final EmailVO vo) throws Exception{
		taskExecutor.execute(new Runnable(){
			public void run(){
				try {
					sendAlternativeMail(vo);
				} catch (Exception e) {
					logger.fatal(e);
				}
			}
		});
	}
	
}
